{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bond featurizers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from chemprop.featurizers.bond import MultiHotBondFeaturizer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is an example bond to featurize."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rdkit import Chem\n",
    "\n",
    "bond_to_featurize = Chem.MolFromSmiles(\"CC\").GetBondBetweenAtoms(0, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bond features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following bond features are generated by `rdkit` and cast to one-hot vectors (except for the initial null bit which is True/False depending on if the bond is `None`). These feature vectors are joined together to a single multi-hot feature vector. Only the stereochemistry vector is padded for unknown values.\n",
    "\n",
    " - null?\n",
    " - bond type\n",
    " - conjugated?\n",
    " - in ring?\n",
    " - stereochemistry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featurizer = MultiHotBondFeaturizer()\n",
    "featurizer(bond_to_featurize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Custom"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The bond types and stereochemistry can be customized. The defaults are:\n",
    "\n",
    " - bond_type\n",
    "    - Single, Double, Triple, Aromatic\n",
    " - stereos\n",
    "    - 0, 1, 2, 3, 4, 5 - See `rdkit.Chem.rdchem.BondStereo` for more details"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 0, 1, 0, 0, 0])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from rdkit.Chem.rdchem import BondType\n",
    "\n",
    "featurizer = MultiHotBondFeaturizer(bond_types=[BondType.SINGLE], stereos=[0, 1, 2])\n",
    "featurizer(bond_to_featurize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Any class that has a length and returns a numpy array when given an `rdkit.Chem.rdchem.Bond` can be used as a bond featurizer. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from rdkit.Chem.rdchem import Bond\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "class MyBondFeaturizer:\n",
    "    def __len__(self):\n",
    "        return 1\n",
    "\n",
    "    def __call__(self, a: Bond):\n",
    "        return np.array([a.GetIsConjugated()], dtype=float)\n",
    "\n",
    "\n",
    "featurizer = MyBondFeaturizer()\n",
    "featurizer(bond_to_featurize)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chemprop",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
